﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Imgur.Net.Auth;
using NUnit.Framework;
using TestableHttpWebResponse;

namespace Imgur.Net.Test.Auth
{
    [TestFixture]
    public class AnonymousAuthContextTests
    {
        [Test]
        public void CreateInstanceString()
        {
            string clientId = "12345";
            var instance = new AnonymousAuthContext(clientId);

            Assert.IsNotNull(instance);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CreateInstanceStringNull()
        {
            string clientId = "";
            var instance = new AnonymousAuthContext(clientId);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void AddContextToRequestNullRequestTest()
        {
            WebRequest req = null;
            string clientId = "12345";


            var instance = new AnonymousAuthContext(clientId);

            Assert.IsNotNull(instance);

            instance.AddContextToRequest(ref req);
        }

        [Test]
        public void AddContextToRequestTest()
        {
            const string testUri = "http://example.com";
            WebRequest req = TestableWebRequest.Create(testUri);

            Assert.IsNotNull(req);
            string clientId = "12345";

            var instance = new AnonymousAuthContext(clientId);

            Assert.IsNotNull(instance);

            instance.AddContextToRequest(ref req);

            string authorizationHeader = req.Headers["Authorization"];

            Assert.IsNotNull(authorizationHeader);
            StringAssert.StartsWith("Client-ID", authorizationHeader);
            StringAssert.Contains(clientId, authorizationHeader);
        }
    }
}
